﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using RTUParameterConfig.Class;

namespace RTUParameterConfig
{
    public partial class WaterLevelsSetting : Form
    {
        public WaterLevelsSetting()
        {
            InitializeComponent();
            SetValue();
        }

        public void InitShow(int intWaterSelect)
        {
            if (intWaterSelect != 6)
            {
                labelControl60.Visible = false;
                labelControl56.Visible = false;
                nud4.Visible = false;
                nud5.Visible = false;
            }
        }

        /// <summary>
        /// 设置水位计配置
        /// </summary>
        public void SetValue()
        {
            try
            {
                nud1.Value = Convert.ToDecimal(BitConverter.ToInt32(ParameterConfig.rtuConfigInfo.MultiSpeedWaterBaseLevel.Reverse<byte>().ToArray(), 0) / 1000.0);
                nud2.Value = Convert.ToDecimal(BitConverter.ToInt32(ParameterConfig.rtuConfigInfo.MultiSpeedWaterCorrectionLevel.Reverse<byte>().ToArray(), 0) / 1000.0);
                nud3.Value = Convert.ToDecimal(BitConverter.ToUInt32(ParameterConfig.rtuConfigInfo.MultiSpeedWaterCorrParame.Reverse<byte>().ToArray(), 0) / 10000.0);
                nud4.Value = Convert.ToDecimal(BitConverter.ToInt32(ParameterConfig.rtuConfigInfo.MultiSpeedDeviation1.Reverse<byte>().ToArray(), 0) / 1000.0);
                nud5.Value = Convert.ToDecimal(BitConverter.ToUInt32(ParameterConfig.rtuConfigInfo.MultiSpeedRatio1.Reverse<byte>().ToArray(), 0) / 10000.0);
            }
            catch
            {
                Common.ShowErrorMessage("加载高级设置参数失败！");
            }
        }

        /// <summary>
        /// 获取水位计配置
        /// </summary>
        public void GetValue()
        {
            ParameterConfig.rtuConfigInfo.MultiSpeedWaterBaseLevel = BitConverter.GetBytes(Convert.ToInt32(nud1.Value * 1000)).Reverse<byte>().ToArray();
            ParameterConfig.rtuConfigInfo.MultiSpeedWaterCorrectionLevel = BitConverter.GetBytes(Convert.ToInt32(nud2.Value * 1000)).Reverse<byte>().ToArray();
            ParameterConfig.rtuConfigInfo.MultiSpeedWaterCorrParame = BitConverter.GetBytes(Convert.ToUInt32(nud3.Value * 10000)).Reverse<byte>().ToArray();
            ParameterConfig.rtuConfigInfo.MultiSpeedDeviation1 = BitConverter.GetBytes(Convert.ToInt32(nud4.Value * 1000)).Reverse<byte>().ToArray();
            ParameterConfig.rtuConfigInfo.MultiSpeedRatio1 = BitConverter.GetBytes(Convert.ToUInt32(nud5.Value * 10000)).Reverse<byte>().ToArray();
        }

        /// <summary>
        /// 关闭前保存修改的配置信息
        /// </summary>
        private void WaterLevelSetting_FormClosing(object sender, FormClosingEventArgs e)
        {
            GetValue();
            this.Dispose();
        }
    }
}
